package org.itcast.travel.controller;

import lombok.extern.slf4j.Slf4j;
import org.itcast.travel.pojo.Blog;
import org.itcast.travel.pojo.BlogQueryParam;
import org.itcast.travel.pojo.PageBean;
import org.itcast.travel.pojo.Result;
import org.itcast.travel.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/blog")
@Slf4j
public class BlogController {
    @Autowired
    private BlogService blogService;

    /**
     * 博客分页条件查询
     *
     * @param blogQueryParam 分页条件查询条件
     * @return 返回对应条数数据
     */
    @GetMapping
    public Result page(BlogQueryParam blogQueryParam) {
        PageBean pageBean = blogService.page(blogQueryParam);
        log.info("返回数据：{}",pageBean);
        return Result.success(pageBean);
    }


    /**
     * 博客根据用户ID分页条件查询
     *
     * @param userId         用户ID
     * @param blogQueryParam 分页查询条件
     * @return 返回所查数据
     */
    @GetMapping("/page/{userId}")
    public Result pageById(@PathVariable Integer userId, BlogQueryParam blogQueryParam) {
        PageBean pageBean = blogService.pageById(userId, blogQueryParam);
        return Result.success(pageBean);
    }

    /**
     * 根据blogId删除博客
     *
     * @param ids 需要删除的blogId
     * @return 无需返回值
     */
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids) {
        blogService.delete(ids);
        return Result.success();
    }

    /**
     * 新增博客
     *
     * @param blog 新增blog对象数据
     * @return 无
     */
    @PostMapping
    public Result add(@RequestBody Blog blog) {
        log.info("请求数据：{}", blog);
        blogService.add(blog);
        return Result.success();
    }

    /**
     * 根据博客ID查询博客，回显数据,查看详情
     *
     * @param blogId 博客ID
     * @return 单条回显数据
     */
    @GetMapping("/{blogId}")
    public Result getInfo(@PathVariable Integer blogId) {
        Blog blog = blogService.getById(blogId);
        log.info("响应数据：{}", blog);
        return Result.success(blog);
    }

    /**
     * 修改博客
     * @param blog 需要修改的博客数据
     * @return 无需返回数据
     */
    @PutMapping
    public Result update(@RequestBody Blog blog) {
        log.info("获取数据：{}",blog);
        blogService.update(blog);
        return Result.success();
    }

    /**
     * 查看博客个人点赞情况
     * @param blogId 博客id
     * @param userId 用户id
     * @return 是否点赞：0未点赞，1已点赞
     */
    @GetMapping("/like")
    public Result getLike(@RequestParam(value = "blogId") Integer blogId,
                          @RequestParam(value = "userId") Integer userId) {
        Integer like = blogService.selectOneLike(blogId,userId);
        return Result.success(like);
    }

    /**
     * 博客点赞
     * @param blogId 点赞的博客id
     * @param userId 点赞的用户id
     * @return 无返回数据
     */
    @PutMapping("/like")
    public Result updateLike(@RequestParam(value = "blogId") Integer blogId,
                             @RequestParam(value = "userId") Integer userId) {
        blogService.updateLike(blogId,userId);
        return Result.success();
    }

    /**
     * 根据用户id分页查询所有点赞博客
     * @param userId 用户id
     * @param blogQueryParam 条件分页查询条件
     * @return 分页条件查询结果
     */
    @GetMapping("/like/{userId}")
    public Result getLike(@PathVariable Integer userId,BlogQueryParam blogQueryParam) {
        PageBean pageBean = blogService.getLike(blogQueryParam,userId);
        return Result.success(pageBean);
    }

    /**
     * 查看博客个人收藏情况
     * @param blogId 博客id
     * @param userId 用户id
     * @return 是否点赞：0未收藏，1已收藏
     */
    @GetMapping("/collect")
    public Result getCollect(@RequestParam(value = "blogId") Integer blogId,
                          @RequestParam(value = "userId") Integer userId) {
        Integer collect = blogService.selectOneCollect(blogId,userId);
        return Result.success(collect);
    }

    /**
     * 博客收藏
     * @param blogId 收藏的博客id
     * @param userId 收藏的用户id
     * @return 无返回数据
     */
    @PutMapping("/collect")
    public Result updateBlogCollect(@RequestParam(value = "blogId") Integer blogId,
                             @RequestParam(value = "userId") Integer userId) {
        blogService.updateCollect(blogId,userId);
        return Result.success();
    }

    /**
     * 根据用户id分页查询所有收藏博客
     * @param userId 用户id
     * @param blogQueryParam 条件分页查询条件
     * @return 分页条件查询结果
     */
    @GetMapping("/collect/{userId}")
    public Result getCollect(@PathVariable Integer userId,BlogQueryParam blogQueryParam) {
        PageBean pageBean = blogService.getCollect(blogQueryParam,userId);
        return Result.success(pageBean);
    }


    /**
     * 用户浏览博客
     * @param blogId 浏览的博客id
     * @param userId 浏览的用户id
     * @return 无返回数据
     */
    @PutMapping("/look")
    public Result updateCollect(@RequestParam(value = "blogId") Integer blogId,
                                @RequestParam(value = "userId") Integer userId) {
        blogService.updateLook(blogId,userId);
        return Result.success();
    }

    /**
     * 根据用户id分页查询所有浏览博客
     * @param userId 用户id
     * @param blogQueryParam 条件分页查询条件
     * @return 分页条件查询结果
     */
    @GetMapping("/look/{userId}")
    public Result getLook(@PathVariable Integer userId,BlogQueryParam blogQueryParam) {
        PageBean pageBean = blogService.getLook(blogQueryParam,userId);
        return Result.success(pageBean);
    }

}
